package com.su.gulimall.search.thread;

import java.util.concurrent.*;

public class Combine {
    public static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,200,10,
            TimeUnit.SECONDS,new LinkedBlockingDeque<>(100000),
            Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1开始:" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("运行结果为：" + i);
            return i;
        }, executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2开始:" + Thread.currentThread().getId());
            return "hello";
        }, executor);
        //runAfterBothAsync:两个任务结合后，第三个任务执行，第三个任务不能感知前面两个的执行结果
     future01.runAfterBothAsync(future02,()->{
                 System.out.println("任务3结束");},executor);
        //runAcceptBothAsync:两个任务结合后，第三个任务执行，第三个任务可以感知前面两个的执行结果
     future01.thenAcceptBothAsync(future02,
                (f1,f2)->{ System.out.println("任务3感知任务1,2的参数："+f1+""+f2); },executor);
        //thenCombineAsync:两个任务结合后，第三个任务执行，第三个任务可以感知前面两个的执行结果
     CompletableFuture<String> future = future01.thenCombineAsync(future02,
                (f1, f2) -> {
                    return f1 + ":" + f2 + "haha";
                }, executor);
        System.out.println("拿到任务3执行后的数据"+future.get());
    }
}
